package com.zh.custom.web;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.alibaba.fastjson.JSONValidator;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.web.BaseController;
import com.zh.custom.entity.Tcustomer;
import com.zh.custom.service.TcustomerService;

/**
 * 客户信息表Controller
 * @author 690378361@163.com
 * @version 2025-08-31
 */
@Controller
@RequestMapping(value = "${adminPath}/custom/customer")
public class CustomerController extends BaseController {

	@Autowired
	private TcustomerService tcustomerService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Tcustomer get(Integer tid, boolean isNewRecord) {
		return tcustomerService.get(tid, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("t:customer:view")
	@RequestMapping(value = {"list", ""})
	public String list(Tcustomer tcustomer, Model model) {
		model.addAttribute("tcustomer", tcustomer);
		return "/modules/custom/tcustomerList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("t:customer:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Tcustomer> listData(Tcustomer tcustomer, HttpServletRequest request, HttpServletResponse response) {
		tcustomer.setPage(new Page<>(request, response));
		Page<Tcustomer> page = tcustomerService.findPage(tcustomer);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("t:customer:view")
	@RequestMapping(value = "form")
	public String form(Tcustomer tcustomer, Model model) {
		model.addAttribute("tcustomer", tcustomer);
		return "modules/custom/tcustomerForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("t:customer:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Tcustomer tcustomer) {
		tcustomerService.save(tcustomer);
		return renderResult(Global.TRUE, text("保存客户信息表成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("t:customer:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(Tcustomer tcustomer) {
		tcustomer.setStatus(Tcustomer.STATUS_DISABLE);
		tcustomerService.updateStatus(tcustomer);
		return renderResult(Global.TRUE, text("停用客户信息表成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("t:customer:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(Tcustomer tcustomer) {
		tcustomer.setStatus(Tcustomer.STATUS_NORMAL);
		tcustomerService.updateStatus(tcustomer);
		return renderResult(Global.TRUE, text("启用客户信息表成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("t:customer:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Tcustomer tcustomer) {
		tcustomerService.delete(tcustomer);
		return renderResult(Global.TRUE, text("删除客户信息表成功！"));
	}
	
	/**
	 * 列表选择对话框
	 */
	@RequiresPermissions("t:customer:view")
	@RequestMapping(value = "tcustomerSelect")
	public String tcustomerSelect(Tcustomer tcustomer, String selectData, Model model) {
		String selectDataJson = EncodeUtils.decodeUrl(selectData);
		if (selectDataJson != null && JSONValidator.from(selectDataJson).validate()){
			model.addAttribute("selectData", selectDataJson);
		}
		model.addAttribute("tcustomer", tcustomer);
		return "modules/custom/tcustomerSelect";
	}
	
}